package study.sort;

import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int[] arr = {101, 34, 49, 1, -2, 3, 22, -9};
        int[] arr2 = {101, 34, 49, 1, -2, 3, 22, -9};

        int[] arr3 = new int[80000];
        int[] arr4 = new int[80000];
        for (int i = 0; i < arr3.length; i++) {
            arr3[i] = (int)(Math.random() * 8000000);
            arr4[i] = (int)(Math.random() * 8000000);
        }
        //System.out.println(Arrays.toString(arr3));
        //System.out.println(Arrays.toString(arr4));

        long startTime3 = System.currentTimeMillis();
        selectSort(arr3);
        long endTime3 = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime3 - startTime3));

        System.out.println("\n-------------------\n");

        long startTime4 = System.currentTimeMillis();
        selectSort2(arr4);
        long endTime4 = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime4 - startTime4));


    }

    /**
     * 最简单粗暴的排序
     * 选择第一个为最小，
     * @param arr
     */
    public static void selectSort(int[] arr) {
        int temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i; j < arr.length - 1; j++) {
                if (arr[i] > arr[j + 1]) {
                    temp = arr[i];
                    arr[i] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }

            //System.out.println("第" + (i + 1) + "轮排序：" + Arrays.toString(arr));
        }
    }

    public static void selectSort2(int[] arr) {
        System.out.println("排序前：" + Arrays.toString(arr));
        int minIndex = 0;
        int minValue = arr[0];
        for (int i = 0; i < arr.length - 1; i++) {
            minIndex = i;
            minValue = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < minValue) {
                    minIndex = j;
                    minValue = arr[j];
                }
            }
            if (minIndex != i){
                arr[minIndex] = arr[i];
                arr[i] = minValue;
            }
            System.out.println("第" + (i + 1) + "轮排序：" + Arrays.toString(arr));
        }
    }


}
